<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <template #title>
        <div class="title-wrap">
          <span class="title">我的频道</span>
          <span class="tip">点击进入频道</span>
        </div>
      </template>
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        :class="{ active: index === active }"
        v-for="(channel, index) in userChannels"
        :key="channel.id"
        @click="onChannelActiveOrDelete(channel, index)"
      >
        <template #text>
          <div>
            <van-icon
              class="close-icon"
              name="close"
              v-show="isEdit && channel.name !== '推荐'"
            />
            {{ channel.name }}
          </div>
        </template>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <template #title>
        <div class="title-wrap">
          <span class="title">推荐频道</span>
          <span class="tip">点击添加频道</span>
        </div>
      </template>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="channelItem in recommendChannels"
        :key="channelItem.id"
        @click="addChannel(channelItem)"
      >
        <template #text>
          <div>
            {{ channelItem.name }}
          </div>
        </template>
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getAllChannels, addChannel, deleteChannel } from '@/api/channel'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allchannels: [],
      isEdit: false
    }
  },
  computed: {
    ...mapState('loginStore', ['user']),
    // 推荐频道
    recommendChannels() {
      return this.allchannels.filter(item => {
        return !this.userChannels.find(userItem => {
          return userItem.id === item.id
        })
      })
    }
  },
  watch: {},
  created() {
    this.getAllChannels()
  },
  methods: {
    async getAllChannels() {
      const { data } = await getAllChannels()
      this.allchannels = data.data.channels
    },
    async addChannel(channel) {
      this.userChannels.push(channel)
      if (this.user) {
        await addChannel({ id: channel.id, seq: this.userChannels.length })
      } else {
        setItem('user-channels', this.userChannels)
      }
    },
    onChannelActiveOrDelete(channel, index) {
      if (this.isEdit && channel.name !== '推荐') {
        this.deleteChannel(channel, index)
      } else {
        this.switchChannel(index)
      }
    },
    async deleteChannel(channel, index) {
      if (index < this.active) {
        this.$emit('updateActive', this.active - 1)
      }
      this.userChannels.splice(index, 1)
      if (this.user) {
        await deleteChannel(channel.id)
      } else {
        setItem('user-channels', this.userChannels)
      }
    },
    switchChannel(index) {
      // 子传父
      this.$emit('updateActive', index)
      this.$emit('close')
    }
  }
}
</script>

<style lang="less">
.title-wrap {
  .title {
    margin-right: 10px;
    font-size: 16px;
  }
  .tip {
    font-size: 12px;
    color: #ccc;
  }
}
.channel-item {
  position: relative;
  height: 43px;
  ::v-deep .van-grid-item__content {
    background: #f4f5f6;
  }
  .text {
    font-size: 14px;
    color: #222;
  }
  .close-icon {
    font-size: 15px;
    position: absolute;
    right: -3px;
    top: -4px;
  }
}
.active {
  .van-grid-item__content {
    color: #e5615b !important;
  }
}
</style>